package com.study.leetcode;

public class Solution_13 {

    public static String intToRoman1(int num) {
        StringBuilder s = new StringBuilder();
        int[] m1 = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1};
        String[] m2 = {"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"};
        int count;
        for (int i = 0; i < m1.length; i++) {
            count = num / m1[i];
            for (int j = 0; j < count; j++) {
                s.append(m2[i]);
            }
            num = num % m1[i];
        }
        return s.toString();
    }

    public static String intToRoman(int num) {
        StringBuilder s = new StringBuilder();
        while (num > 0) {
            if (num - 1000 >= 0) {
                s.append("M");
                num -= 1000;
            } else if (num - 900 >= 0) {
                s.append("CM");
                num -= 900;
            } else if (num - 500 >= 0) {
                s.append("D");
                num -= 500;
            } else if (num - 400 >= 0) {
                s.append("CD");
                num -= 400;
            } else if (num - 100 >= 0) {
                s.append("C");
                num -= 100;
            } else if (num - 90 >= 0) {
                s.append("XC");
                num -= 90;
            } else if (num - 50 >= 0) {
                s.append("L");
                num -= 50;
            } else if (num - 40 >= 0) {
                s.append("XL");
                num -= 40;
            } else if (num - 10 >= 0) {
                s.append("X");
                num -= 10;
            } else if (num - 9 >= 0) {
                s.append("IX");
                num -= 9;
            } else if (num - 5 >= 0) {
                s.append("V");
                num -= 5;
            } else if (num - 4 >= 0) {
                s.append("IV");
                num -= 4;
            } else if (num - 1 >= 0) {
                s.append("I");
                num -= 1;
            }
        }
        return s.toString();
    }

    public static int romanToInt(String s) {
        int r = 0;
        int len = s.length() - 1;
        int i = 0;
        char c;
        char n;
        while (i <= len) {
            c = s.charAt(i);
            n = '\0';
            if (i + 1 <= len) {
                n = s.charAt(i + 1);
            }
            if (c == 'M') {
                r = r + 1000;
            } else if (c == 'D') {
                r = r + 500;
            } else if (c == 'C') {
                if (n == 'D') {
                    r = r + 400;
                    i = i + 1;
                } else if (n == 'M') {
                    r = r + 900;
                    i = i + 1;
                } else {
                    r = r + 100;
                }
            } else if (c == 'L') {
                r = r + 50;
            } else if (c == 'X') {
                if (n == 'L') {
                    r = r + 40;
                    i = i + 1;
                } else if (n == 'C') {
                    r = r + 90;
                    i = i + 1;
                } else {
                    r = r + 10;
                }

            } else if (c == 'V') {
                r = r + 5;
            } else if (c == 'I') {
                if (n == 'V') {
                    r = r + 4;
                    i = i + 1;
                } else if (n == 'X') {
                    r = r + 9;
                    i = i + 1;
                } else {
                    r = r + 1;
                }
            }
            i++;
        }
        return r;
    }

    public static void main(String[] args) {
        String roman = "III";
        int toInt = romanToInt(roman);
        System.out.println("toInt = " + toInt);
        String toRoman = intToRoman1(toInt);
        System.out.println(roman + "," + toRoman);

    }
}
